﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using Ext.Net;

namespace BitPortal.Admin.Service.Product
{
	public partial class Default1 : Controls.BaseAdminPage
	{
        protected override void OnInit(EventArgs e)
        {
            // PermissionBLL.CheckRoleThrowException(role: "ProductCustomerView", useExceptionHandler: true);
            base.OnInit(e);
            (this.errorAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
            
        }



        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<Models.ProductCustomer> data;
            Guid userID = (Guid)Membership.GetUser().ProviderUserKey;
            if (Session["ProductCustomerGridFlag"] != null && Session["ProductCustomerGridFlag"].Equals(false))
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                data = bitEnt.ProductCustomers.Where(ent => ent.IsDeleted == true).ToList();
            }
            else
            {
                data = ProductCustomerBLL.GetAllByUser(userID).ToList();
            }
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<BitPortal.Models.ProductCustomer>(data, fc, e);
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            try
            {
                //PermissionBLL.CheckRoleThrowException(role: "ErrorInsert", useExceptionHandler: true);
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                if (sm.SelectedRows.Count > 0)
                {
                    long ID = Convert.ToInt64(sm.SelectedRows[0].RecordID);
                    errorAddEdit1.CreateError(ID);
                    ShowNotification(ID.ToString());
                    LoadGridData();

                }
            }
            catch (SystemException ex)
            {
                ShowNotification(ex.Message);
            }
           
        }

        [DirectMethod]
        protected void btnViewAllError_Click(object sender, DirectEventArgs e)
        {
            try
            {
                //PermissionBLL.CheckRoleThrowException(role: "ErrorView", useExceptionHandler: true);
                Guid userID = (Guid)Membership.GetUser().ProviderUserKey;
                wProductError.AutoLoad.Url = "/Admin/Service/Errors/?UserId="+userID.ToString();
                wProductError.LoadContent();
                wProductError.Show();
            }
            catch (SystemException ex)
            {
                ShowNotification(ex.Message, "Thông báo",Icon.ApplicationError);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            
        }

        protected void btnTrashEmpty_Click(object sender, DirectEventArgs e)
        {

        }

        protected void btnTrashDelete_Click(object sender, DirectEventArgs e)
        {

        }

        protected void btnUnDelete_Click(object sender, DirectEventArgs e)
        {

        }

        protected void btnTrash_Click(object sender, DirectEventArgs e)
        {

        }

        [DirectMethod]
        public void LoadNotes(string id)
        {
            wProductNotes.AutoLoad.Url = "/Admin/Service/Errors/?ProductCustomerId=" + id;
            wProductNotes.LoadContent();
            wProductNotes.Show();
        }
	}
}